/**
 * description: 工具文件
 * author: 83
 * createTime: 2021-11-5 2:11:57 ?F10: PM?
 */

// promisify 模态窗
export const promisifyShowModal = (options) => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			...options,
			success: resolve,
			fail: reject
		})
	})
}

// promisify 选择图片
export const promisifyChooseImage = (options) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			...options,
			success: resolve,
			fail: reject
		})
	})
}

// promisify 选择视频
export const promisifyChooseVideo = (options) => {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			...options,
			success: resolve,
			fail: reject
		})
	})
}

/**
 * description: 获取8种数据类型
 * author: 83
 * createTime: 2021-11-5 2:20:44 ?F10: PM?
 * @param {any}
 * @return {string}
 */
export const getDataType = (val) => {
	let ans = typeof val
	if (ans === 'object') {
		ans = Object.prototype.toString.call(val).slice(8, -1).toLowerCase()
	}
	return ans
}

/**
 * 自定义时间格式化
 * @param {Date|string} time 
 * @param {string} reg 
 */
export const formatTime = (time, reg) => {
  if (!time) return '-'
  const date = typeof time === 'string' ? new Date(time.replace(/-/g, '/')) : time
  const map = {}
  map.yyyy = date.getFullYear()
  map.yy = ('' + map.yyyy).substr(2)
  map.M = date.getMonth() + 1
  map.MM = formatNumber(map.M)
  map.d = date.getDate()
  map.dd = formatNumber(map.d)
  map.H = date.getHours()
  map.HH = formatNumber(map.H)
  map.m = date.getMinutes()
  map.mm = formatNumber(map.m)
  map.s = date.getSeconds()
  map.ss = formatNumber(map.s)
	map.AMPM = map.H > 12 ? 'PM' : 'AM'
  return reg.replace(/\byyyy|yy|MM|M|dd|d|HH|H|mm|m|ss|s|AMPM\b/g, $1 => map[$1])
}

/**
 * 个数补零
 * @param {number} num 
 * @return {string}
 */
export const formatNumber = (num) => {
  num = num + ''
  return num[1] ? num : `0${num}`
}

/**
 * 睡眠
 * @param {number} delay 延迟毫秒 
 */
export const sleep = (delay = 2000) => {
  return new Promise((resolve) => {
    setTimeout(resolve, delay)
  })
}